import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import vuetify from "./plugins/vuetify";
import animated from "animate.css";
import "./assets/css/index.css";
import "./assets/css/iconfont.css";
import "./assets/css/markdown.css";
import "./assets/css/vue-social-share/client.css";
import config from "./assets/js/config";
import Share from "vue-social-share";
import dayjs from "dayjs";
import { vueBaberrage } from "vue-baberrage";
import axios from "axios";
import VueAxios from "vue-axios";
import InfiniteLoading from "vue-infinite-loading";
import "highlight.js/styles/atom-one-dark.css";
import VueImageSwipe from "vue-image-swipe";
import "vue-image-swipe/dist/vue-image-swipe.css";
import Toast from "./components/toast/index";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import ElementUI from "element-ui";
import * as authUtils from "./utils/authUtils"

Vue.prototype.config = config;
Vue.config.productionTip = false;
Vue.use(animated);
Vue.use(Share);
Vue.use(vueBaberrage);
Vue.use(InfiniteLoading);
Vue.use(VueAxios, axios);
Vue.use(VueImageSwipe);
Vue.use(Toast);
Vue.use(ElementUI);

Vue.filter("date", function(value) {
  return dayjs(value).format("YYYY-MM-DD");
});

Vue.filter("year", function(value) {
  return dayjs(value).format("YYYY");
});

Vue.filter("hour", function(value) {
  return dayjs(value).format("HH:mm:ss");
});

Vue.filter("time", function(value) {
  return dayjs(value).format("YYYY-MM-DD HH:mm:ss");
});

Vue.filter("num", function(value) {
  if (value >= 1000) {
    return (value / 1000).toFixed(1) + "k";
  }
  return value;
});

router.beforeEach((to, from, next) => {
  NProgress.start();

  if(authUtils.isExpire()){
    store.commit("flushLoginDate");
  }else{
    store.commit("flushAll");
  }
  
  if (to.path === "/user" && (store.state.userId === null || store.state.token === null)) {
    next({ path: "/" });
  }else {
    if (to.meta.title) {
      document.title = to.meta.title;
    }
    next();
  }

});

router.afterEach(() => {
  window.scrollTo({
    top: 0,
    behavior: "instant"
  });
  NProgress.done();
});


axios.interceptors.request.use(
  (config) => {

    if(authUtils.isExpire()){
      store.commit("flushLoginDate");
    }else{
      store.commit("removeToken");
    }

    if(store.state.token !== null){
      config.headers["token"] = store.state.token;
    }
    return config;
  }   
);

axios.interceptors.response.use(
  function(response) {

    // const code = response.data.code;
    // const message = response.data.message;

    return response;

  },
  function(error) {

    var data = (error.response || {}).data || {};
    var code = data.code || (error.response || error).status || {};
    var msg = data.message || error.message;

    if(authUtils.isExpire()){
      store.commit("flushLoginDate");
      if (code !== 404 && code !== 500 && code !== 403) {
        store.commit("flushAll");
        router.push({ path: "/" });
      }
    }else if(store.state.loginDate !== null){
      if(store.state.token === null){
        msg = "当前登录状态已过期";
      }
      store.commit("flushAll");
      router.push({ path: "/" });
    }

    Vue.prototype.$toast({ type: "error", message: msg });

    return Promise.reject(error);
  }
);

new Vue({
  router,
  store,
  vuetify,
  render: h => h(App)
}).$mount("#app");
